﻿using System.Linq;
using IterationLife.ContentResources.Models;
using Util;
using Util.Maps;

namespace IterationLife.PlatformBackEnd.AppServices.Dtos.ContentResources
{
    /// <summary>
    /// 内容资源数据传输对象扩展
    /// </summary>
    public static class ContentResourceDtoExtension
    {
        /// <summary>
        /// 转换为内容资源实体
        /// </summary>
        /// <param name="dto">内容资源数据传输对象</param>
        public static ContentResource ToEntity(this ContentResourceDto dto)
        {
            if (dto == null)
                return new ContentResource();
            var entity = dto.MapTo(new ContentResource(dto.Id.ToGuid()));
            entity.Tags = dto.TagList.Join();
            return entity;
        }

        /// <summary>
        /// 转换为内容资源数据传输对象
        /// </summary>
        /// <param name="entity">内容资源实体</param>
        public static ContentResourceDto ToDto(this ContentResource entity)
        {
            if (entity == null)
                return new ContentResourceDto();
            var dto = entity.MapTo(new ContentResourceDto());
            dto.PictureDtos = entity.ContentResourcePictures.Select(t => t.ToDto()).OrderBy(t => t.SortId).ToList();
            dto.TagList = Util.Helpers.Convert.ToList<string>(entity.Tags);
            return dto;
        }
    }
}
